class Burndown < ActiveRecord::Base
  belongs_to :account
  validates_presence_of :name
  validates_presence_of :project_id
  validates_presence_of :project_name
  validates_presence_of :milestone_id
  validates_presence_of :milestone_title
  validates_presence_of :milestone_deadline
  validates_presence_of :account_id


  def before_validation
    a = Account.find(self.account_id)
    @basecamp = BasecampWrapper.create_wrapper
    @basecamp.connect!(a)
    assign_project
    assign_milestone
  end
    
  private
  def assign_project
    @basecamp.projects.each do |p|
      if p.id == self.project_id then
        self.project_id = p.id
        self.project_name = p.name
      end
    end
  end
  
  def assign_milestone
    @basecamp.milestones(self.project_id).each do |m|
      if m.id == self.milestone_id
        self.milestone_id = m.id
        self.milestone_title = m.title
        self.milestone_deadline = m.deadline
      end
    end
  end
  
end
